from django.db import models

# Create your models here.
from Baby.models import Users


class Hospital(models.Model):
    hospital_name = models.CharField(max_length=100, blank=False, null=False)
    hospital_address = models.CharField(max_length=100, blank=False, null=False)
    hospital_logo = models.CharField(max_length=100, blank=False, null=False)
    hospital_url = models.CharField(max_length=255, default="",  blank=False, null=False)
    is_delete = models.BooleanField(default=False, verbose_name="whether it was deleted")

    class Meta:
        db_table = 'hospital'
        verbose_name = 'Hospital Information'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.hospital_name

    def hospital_to_json(self):
        result = {
            "id": self.id,
            "hospital_name": self.hospital_name
        }
        return result


class Source(models.Model):
    id = models.AutoField(blank=False, null=False, primary_key=True)
    name = models.CharField(max_length=100, blank=True, null=True)
    logo = models.CharField(max_length=255, blank=True, null=True)
    target = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        db_table = 'source'

    def source_to_json(self):
        result = {
            "id": self.id,
            "name": self.name,
            "logo": self.logo,
            "target": self.target,
        }
        return result


class EducationCategory(models.Model):
    cid = models.CharField(max_length=64, blank=True, null=True)
    parent_id = models.CharField(max_length=64, blank=True, null=True)
    title = models.CharField(max_length=255, blank=True, null=True)
    title_SP = models.CharField(max_length=255, blank=True, null=True)
    order_id = models.IntegerField(blank=True, null=True)
    level = models.IntegerField(blank=True, null=True)
    hospital = models.ForeignKey(
        to='Hospital',
        db_constraint=False,
        on_delete=models.CASCADE
    )
    created_at = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        db_table = 'educationcategory'
        verbose_name = 'educationcategory Information'
        verbose_name_plural = verbose_name
        ordering = ['order_id']

    @staticmethod
    def get_parent_array(current_category_id, hospital_id):

        result = []
        get_super = True
        while get_super:
            r = EducationCategory.objects.filter(cid=current_category_id, hospital_id=hospital_id).first()
            if not r: return result
            ret = {'id': r.cid, 'pid': r.parent_id, 'title': r.title, 'title_SP': r.title_SP, 'type': 0}

            result.append(ret)
            current_category_id = r.parent_id
            if r.parent_id == 'root':
                break
        return result[::-1]

    def __str__(self):
        return self.title

    def category_to_json(self):
        res = {
            "cid": self.cid,
            "parent_id": self.parent_id,
            "title": self.title,
            "title_SP": self.title_SP,
            "order_id": self.order_id,
            "level": self.level
        }
        return res


class EducationFeeds(models.Model):
    id = models.AutoField(blank=False, null=False, primary_key=True)
    title = models.CharField(max_length=255)
    title_SP = models.CharField(max_length=255)
    content_order = models.IntegerField(blank=True, null=True)
    hospital_id = models.IntegerField(blank=True, null=True)
    category_id = models.CharField(max_length=64, blank=True, null=True)
    summary_1 = models.TextField(blank=True, null=True)
    summary_2 = models.TextField(blank=True, null=True)
    summary_1_SP = models.TextField(blank=True, null=True)
    summary_2_SP = models.TextField(blank=True, null=True)
    source = models.CharField(max_length=255)
    tags = models.CharField(max_length=1000, blank=True, null=True)
    title_media_type = models.CharField(max_length=255, blank=True, null=True)
    title_media_url = models.CharField(max_length=255, blank=True, null=True)
    title_media_url_SP = models.CharField(max_length=255, blank=True, null=True)
    is_delete = models.BooleanField(default=False, verbose_name="whether it was deleted")
    page_info = models.TextField(blank=True, null=True)
    page_info_SP = models.TextField(blank=True, null=True)
    related_content_1 = models.IntegerField(blank=True, null=True)
    related_content_2 = models.IntegerField(blank=True, null=True)
    related_content_3 = models.IntegerField(blank=True, null=True)
    related_content_4 = models.IntegerField(blank=True, null=True)
    time_of_delivery_week = models.IntegerField(blank=True, null=True)
    time_of_delivery_day = models.IntegerField(blank=True, null=True)
    time_of_delivery_hour = models.IntegerField(blank=True, null=True)

    created_at = models.DateTimeField(blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'education_feeds'
        unique_together = [
            ("hospital_id", "category_id"),
        ]
        ordering = ['content_order']

    @staticmethod
    def get_related_content(list):
        try:
            result = []
            for it in list:
                r = []
                record = EducationFeeds.objects.filter(id=it).first()
                if record:
                    content = {'id': record.id, 'cid': record.category_id, 'title': record.title, 'type': 1}
                    r.append(content)
                    super_category = (EducationCategory.get_parent_array(record.category_id))[::-1]
                    r = (r + super_category)[::-1]
                    result.append(r)
                else:
                    result.append([{"id": "", "title": "", "type": ""}])
            return result
        except Exception as e:
            raise e

    def educationFeeds_to_json(self):
        result = {
            "id": self.id if self.id else "",
            "content_order": self.content_order if self.content_order else "",
            "title": self.title if self.title else "",
            "title_SP": self.title_SP if self.title_SP else "",
            "source": self.source if self.source else "",
            "hospital_id": self.hospital_id if self.hospital_id else "",
        }
        return result

    def educationFeeds_details_to_json(self):
        result = {
            "id": self.id,
            "content_order": self.content_order,
            "title": self.title,
            "title_SP": self.title_SP,
            # "source": self.source,
            "source": Source.objects.filter(id=int(self.source)).first().source_to_json(),

            "hospital_id": self.hospital_id,
            "category_id": self.category_id,
            "summary_1": self.summary_1,
            "summary_1_SP": self.summary_1_SP,
            "summary_2": self.summary_2,
            "summary_2_SP": self.summary_2_SP,
            "tags": self.tags,
            "title_media_type": self.title_media_type,
            "title_media_url": self.title_media_url,
            "title_media_url_SP": self.title_media_url_SP,
            "page_info": self.page_info,
            "page_info_SP": self.page_info_SP,
            "related_content_1": self.related_content_1,
            "related_content_2": self.related_content_2,
            "related_content_3": self.related_content_3,
            "related_content_4": self.related_content_4,
            "is_delete": self.is_delete
        }
        return result


class CardAttributes(models.Model):
    user = models.ForeignKey(to=Users, related_name='card_attributes', db_constraint=False, on_delete=models.CASCADE)
    feed = models.ForeignKey(to=EducationFeeds, related_name='card_attributes', db_constraint=False,
                             on_delete=models.CASCADE)
    post_id = models.IntegerField(blank=True, null=True)
    milestone_id = models.IntegerField(blank=True, null=True)
    card_attribute = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    class Meta:
        db_table = 'card_attributes'

    def card_attributes_to_json(self):
        result = {
            "id": self.id,
            "feed_id": self.feed_id,
            "post_id": self.post_id,
            "milestone_id": self.milestone_id,
            "user_id": self.user_id,
            "card_attribute": self.card_attribute,
        }
        return result


class RecommendedContents(models.Model):
    education_feed_id = models.IntegerField(blank=True, null=True)
    users_id = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'recommended_contents'
        ordering = ['-id']
